
/*
  igo, veja ai dentro dessa thread o método run()
  veja os trechos de código comentados pra vc ter um idéia de como eu tva pensando (como te mostrei no busu)
 * dentro deste run() tem as condições (os ifs) para todas as direções (up, left...)
 * em cada condição desta eu pego a distancia do sonar da interface (variável distIR ) e a distÂncia do infravermelho da interface tb ( variável distSonar)
 * o ângulo tá dando problema, deixa ele comentado, como está lá
 * ai em seguida em cada condição tem a velocidade proporcionada pela saida.
 * veja q minha ideia é fuzzificar valores de (distIR, distSonar, sonarAng) e depois deffuzificar esse valor gerando a velocidade
como o ângulo num tah pegando certo, coloque um valor de Ãngulo fixo para todos tipo 29
 * ficaria algo como
 *    velocidade = obj.defuzzyficar(obj.fuzzyficar(distIR, distSonar, 29));




*/





package Gui;

import controladorfuzzy.ControladorFuzzy;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import controladorfuzzy.Fuzzificacao;
import controladorfuzzy.Defuzzificacao;
/*
 * Interface.java
 *
 * Created on 9 de Janeiro de 2010, 17:15
 */

public class Interface extends javax.swing.JFrame {

    /** Creates new form Interface */
    public Interface() { //construtor

        initComponents();
        robotPanel.requestFocus();
        roboPanel.setLocation(350, 350);
        robotPanel.setLocation(0,0);
        roboPanel.setOpaque(true);
        roboPanel.setBounds(350, 350, 35, 35);
        roboPanel.updateUI();
        repaint();
        limites = new Rectangle();
        limites = robotPanel.getBounds();
        verificaLimites = new Thread(new Disparar());
        verificaLimites.start();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jPanel4 = new javax.swing.JPanel();
        text = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jPanel5 = new javax.swing.JPanel();
        jTextField1 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jTextField2 = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jTextField3 = new javax.swing.JTextField();
        jTextField4 = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        robotPanel = new Gui.GridMap();
        roboPanel = new Gui.Robot();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Controlador Fuzzy");
        setBackground(new java.awt.Color(204, 204, 255));
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 500, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel4.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        text.setForeground(new java.awt.Color(240, 35, 35));

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addGap(199, 199, 199)
                .addComponent(text, javax.swing.GroupLayout.PREFERRED_SIZE, 554, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(168, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(text, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 28, Short.MAX_VALUE)
        );

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 1, true));

        jPanel5.setBackground(new java.awt.Color(204, 204, 255));
        jPanel5.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createTitledBorder("Informações do Robô")));

        jLabel1.setText(" Velocidade:");

        jLabel2.setText("Distancia IR:");

        jLabel3.setText("Distancia Sonar:");

        jLabel4.setText("Angulo:");

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap(30, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel4)
                    .addComponent(jLabel3)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(26, 26, 26)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(jTextField4)
                    .addComponent(jTextField3)
                    .addComponent(jTextField2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE))
                .addContainerGap(81, Short.MAX_VALUE))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(328, Short.MAX_VALUE))
        );

        robotPanel.setPreferredSize(new java.awt.Dimension(533, 500));
        robotPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                robotPanelMousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                robotPanelMouseReleased(evt);
            }
        });
        robotPanel.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                robotPanelKeyPressed(evt);
            }
        });

        roboPanel.setPreferredSize(new java.awt.Dimension(35, 35));

        javax.swing.GroupLayout roboPanelLayout = new javax.swing.GroupLayout(roboPanel);
        roboPanel.setLayout(roboPanelLayout);
        roboPanelLayout.setHorizontalGroup(
            roboPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 35, Short.MAX_VALUE)
        );
        roboPanelLayout.setVerticalGroup(
            roboPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 35, Short.MAX_VALUE)
        );

        javax.swing.GroupLayout robotPanelLayout = new javax.swing.GroupLayout(robotPanel);
        robotPanel.setLayout(robotPanelLayout);
        robotPanelLayout.setHorizontalGroup(
            robotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(robotPanelLayout.createSequentialGroup()
                .addGap(219, 219, 219)
                .addComponent(roboPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(279, Short.MAX_VALUE))
        );
        robotPanelLayout.setVerticalGroup(
            robotPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(robotPanelLayout.createSequentialGroup()
                .addGap(174, 174, 174)
                .addComponent(roboPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(287, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(robotPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(34, 34, 34)
                        .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(jPanel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(20, 20, 20))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(robotPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void robotPanelKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_robotPanelKeyPressed
        int key = evt.getKeyCode();

        if (key == KeyEvent.VK_UP) { //apertou a tecla para cima
            up = true;
            down = false;
            left = false;
            right = false;

        } else if (key == KeyEvent.VK_DOWN) { //apertou a tecla para baixo
            up = false;
            down = true;
            left = false;
            right = false;

        } else if (key == KeyEvent.VK_LEFT) { //apertou a tecla para esquerda
            up = false;
            down = false;
            left = true;
            right = false;

        } else if (key == KeyEvent.VK_RIGHT) { //apertou a tecla para direita
            up = false;
            down = false;
            left = false;
            right = true;
        }
}//GEN-LAST:event_robotPanelKeyPressed

    private void robotPanelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_robotPanelMousePressed
        // captura o clique inicial do mouse
        Xinicial = evt.getX();
        Yinicial = evt.getY();
    }//GEN-LAST:event_robotPanelMousePressed

    private void robotPanelMouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_robotPanelMouseReleased
        // captura o soltar do clique do mouse
        Xfinal = evt.getX();
        Yfinal = evt.getY();
        robotPanel.desenhaObstaculo(Xinicial, Yinicial, Xfinal, Yfinal);
    }//GEN-LAST:event_robotPanelMouseReleased

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // finaliza a Thread quando estiver saindo do programa
        verificaLimites.stop();
        verificaLimites.suspend();
        verificaLimites.interrupt();
    }//GEN-LAST:event_formWindowClosing
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new Interface().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    private javax.swing.JTextField jTextField4;
    private Gui.Robot roboPanel;
    private Gui.GridMap robotPanel;
    private javax.swing.JLabel text;
    // End of variables declaration//GEN-END:variables
    private boolean up,  down,  left,  right;
    private Rectangle limites;
    Thread verificaLimites;
    boolean colidiuParedeXd;
    boolean colidiuParedeXe;
    boolean colidiuParedeYt;
    boolean colidiuParedeYb;
    int Xinicial, Yinicial, Xfinal, Yfinal;

    public class Disparar extends Thread implements Runnable { //classe para criar a Thread

//       // private Inferencia inferencia;///
//        private Fuzzificacao fuzzyfica;// seriiiiiiiia criada para acessar o método de fuzzificar()
//       private Defuzzificacao desfuzzyfica;// seriiiiiiiia criada para acessar o método de deffuzificar()
        private Colisoes colisao;
        private double distIR;
        private double distSonar;
        private double angulo;
        private double velocidade;

        public Disparar() { //construtor
            colisao = new Colisoes();
        }

        @SuppressWarnings("static-access")
        public void run() { //rodar a Thread ciclicamente
            while (true) {

                // inferencia = new Inferencia();
//                fuzzyfica = new Fuzzificacao();           // essa era a ideia para chamar os métodos direto
//                desfuzzyfica = new Defuzzificacao(null, null) // essa era a ideias para chamar os metodos direto
               
                if(velocidade > 27)
                {
                    jTextField1.setText(String.valueOf(30.00));
                }
                else
                {
                    jTextField1.setText(String.valueOf(velocidade));
                }
                jTextField2.setText(String.valueOf((distIR/0.75)- 33));
                jTextField3.setText(String.valueOf((distSonar/0.75)- 66));
                jTextField4.setText(String.valueOf(angulo));

                // verifica colisoes com as paredes, porem nao considera fuzzy
                try {
                    if (((roboPanel.getLocation().x) + 35 ) >= limites.getMaxX()) {
                        colidiuParedeXd = true;
                    }
                    if (((roboPanel.getLocation().x) - 12 ) <= limites.getMinX()) {
                        colidiuParedeXe = true;
                    }
                    if (((roboPanel.getLocation().y) + 35) >= limites.getMaxY()) {
                        colidiuParedeYb = true;
                    }
                    if (((roboPanel.getLocation().y)- 12 ) <= limites.getMinY()) {
                        colidiuParedeYt = true;
                    }

                    if (up) { //tecla pressionada para cima

                        distIR = colisao.calcularDistanciaIR(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),0); //sentido 0 = up
                        distSonar = colisao.calcularDistanciaSonar(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),0);

    // não descomente //angulo = colisao.calcularAngulo(robotPanel.getLocation(),robotPanel.getArrayObstaculos(),0); // deixe comentado todos os ângulos, o que eu fiz ah desolando demais o triangulo
                        ControladorFuzzy controlador = new ControladorFuzzy(distIR, distSonar, 29);
                        velocidade  = controlador.iniciarControle();
//                        velocidade = desfuzzyfica.defuzzyficar(fuzzyfica.fuzzificar(angulo, distSonar, 29))); // deixe o ângulo com o valor de 29, um valor a menos que o máximo
                           System.out.println("velocidade na interface: " + velocidade);
                        double velocidadeReal = 750/velocidade*2.5;

                        if(velocidade < 9.0)
                        {
                            roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                        }
                        else if(velocidade < 27)
                        {
                            Thread.sleep((long) (velocidadeReal));
                            if (colidiuParedeYt != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y -1);
                                colidiuParedeYb = false;
                            }
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                        else
                        {
                            Thread.sleep(10);
                            if (colidiuParedeYt != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y -1);
                                colidiuParedeYb = false;
                            } 
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                    }
                    if (down) { // tecla pressionada para baixo

                        distIR = colisao.calcularDistanciaIR(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),2); // sentido 2 = down
                        distSonar = colisao.calcularDistanciaSonar(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),2);

    // não descomente //angulo = colisao.calcularAngulo(robotPanel.getLocation(),robotPanel.getArrayObstaculos(),0); // deixe comentado todos os ângulos, o que eu fiz ah desolando demais o triangulo
               //       velocidade = desfuzzyfica.defuzzyficar(fuzzyfica.fuzzificar(angulo, distSonar, angulo))); // deixe o ângulo com o valor de 29, um valor a menos que o máximo

                        ControladorFuzzy controlador = new ControladorFuzzy(distIR, distSonar, 29);
                        velocidade  = controlador.iniciarControle();
                        System.out.println("velocidade na interface: " + velocidade);

                        double velocidadeReal = 750/velocidade*2.5;

                        if(velocidade < 9.0)
                        {
                            roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                        }
                        else if(velocidade < 27)
                        {
                            Thread.sleep((long) (velocidadeReal));

                            if (colidiuParedeYb != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y +1);
                                colidiuParedeYt = false;
                            }
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                        else
                        {
                            Thread.sleep(10);
                            if (colidiuParedeYb != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y +1);
                                colidiuParedeYt = false;
                            } 
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                    }
                    if (left) { // tecla pressionada para a esquerda

                        distIR = colisao.calcularDistanciaIR(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),3); // sentido 3 = left
                        distSonar = colisao.calcularDistanciaSonar(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),3);

    // não descomente //angulo = colisao.calcularAngulo(robotPanel.getLocation(),robotPanel.getArrayObstaculos(),0); // deixe comentado todos os ângulos, o que eu fiz ah desolando demais o triangulo
               //       velocidade = desfuzzyfica.defuzzyficar(fuzzyfica.fuzzificar(angulo, distSonar, angulo))); // deixe o ângulo com o valor de 29, um valor a menos que o máximo

                        ControladorFuzzy controlador = new ControladorFuzzy(distIR, distSonar, 29);
                        velocidade  = controlador.iniciarControle();
                        System.out.println("velocidade na interface: " + velocidade);

                        double velocidadeReal = 750/velocidade*2.5;

                        if(velocidade < 9.0)
                        {
                            roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                        }
                        else if(velocidade < 27)
                        {
                            Thread.sleep((long) (velocidadeReal));
                            if (colidiuParedeXe != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x - 1, roboPanel.getLocation().y);
                                colidiuParedeXd = false;
                            } 
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                        else
                        {
                            Thread.sleep(10);
                            if (colidiuParedeXe != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x - 1, roboPanel.getLocation().y);
                                colidiuParedeXd = false;
                            }
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                    }
                    if (right) { // tecla pressionada para a direita

                        distIR = colisao.calcularDistanciaIR(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),1); // sentido 1 = right
                        distSonar = colisao.calcularDistanciaSonar(roboPanel.getLocation(),robotPanel.getArrayObstaculos(),1);

    // não descomente //angulo = colisao.calcularAngulo(robotPanel.getLocation(),robotPanel.getArrayObstaculos(),0); // deixe comentado todos os ângulos, o que eu fiz ah desolando demais o triangulo
               //       velocidade = desfuzzyfica.defuzzyficar(fuzzyfica.fuzzificar(angulo, distSonar, angulo))); // deixe o ângulo com o valor de 29, um valor a menos que o máximo
                        
                        ControladorFuzzy controlador = new ControladorFuzzy(distIR, distSonar, 29);                        
                        velocidade  = controlador.iniciarControle();
                        System.out.println("velocidade na interface: " + velocidade);
                        double velocidadeReal = 750/velocidade*2.5;
                        
                        if(velocidade < 9.0)
                        {
                            roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                        }
                        else if(velocidade < 27)
                        {
                            Thread.sleep((long) (velocidadeReal));

                            if (colidiuParedeXd != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x + 1, roboPanel.getLocation().y);
                                colidiuParedeXe = false;
                            }
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                        else
                        {
                            Thread.sleep(10);
                            if (colidiuParedeXd != true)
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x + 1, roboPanel.getLocation().y);
                                colidiuParedeXe = false;
                            }
                            else
                            {
                                roboPanel.setLocation(roboPanel.getLocation().x, roboPanel.getLocation().y);
                            }
                        }
                    }
                } catch (InterruptedException ex) {
                    Logger.getLogger(Interface.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }
}
